package org.gg.boom.kernel.validate;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Component
public class ValidatorGroup extends LocalValidatorFactoryBean {

    /**
     * 自定义校验分组信息存储
     */
    private static final ThreadLocal<Class<?>> VALIDATOR_GROUP_THREAD_LOCAL = new ThreadLocal<>();


    @Override
    public void validate(Object target, Errors errors, Object... validationHints) {
        try {
            if (validationHints.length > 0) {
                VALIDATOR_GROUP_THREAD_LOCAL.set((Class<?>)validationHints[0]);
            }
            super.validate(target, errors, validationHints);
        } finally {
            VALIDATOR_GROUP_THREAD_LOCAL.remove();
        }
    }

    /**
     * 获取当前的校验分组信息
     *
     * @return
     */
    public static Class<?> getValidatorGroup() {
        return VALIDATOR_GROUP_THREAD_LOCAL.get();
    }

}
